<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\User;
use App\Models\Fund;
use App\Models\Owner;
use App\Models\OwnerItem;
use App\Services\EstateService;
use Dcat\Admin\Controllers\AdminController;
use Dcat\Admin\IFrameGrid;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Models\Administrator;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\Auth;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

class AdminTestController extends AdminController
{
    public function index(Content $content)
    {
        if (request(IFrameGrid::QUERY_NAME)) {
            return $content->perfectScrollbar()->body($this->iFrameGrid());
        }

        return $content
            ->title("管理员测试面板")
            ->description("开发完毕后，会把该功能的接口和方法注释")
            ->body(view("admin.admin.test"));
    }

    // 清空所有数据
    public function deleteAll()
    {
        DB::table("users")->where("id", "!=","0")->delete();
        DB::table("owners")->where("id", "!=","0")->delete();
        DB::table("houses")->where("id", "!=","0")->delete();
        DB::table("meters")->where("id", "!=","0")->delete();
        DB::table("funds")->where("id", "!=","0")->delete();
        DB::table("sends")->where("id", "!=","0")->delete();
        DB::table("admin_operation_log")->where("id", "!=","0")->delete();
        DB::table("receive_payment_bodies")->where("id", "!=","0")->delete();
        DB::table("receive_payment_headers")->where("id", "!=","0")->delete();
        DB::table("wt_requests")->where("id", "!=","0")->delete();
    }

    // 新增数据
    public function storeData($id)
    {
        Artisan::call("db:seed --class=HouseSeeder");
        Artisan::call("db:seed --class=OwnerSeeder{$id}");
    }

    /**
     * 根据owner的 “will_close字段” 和 “合同关闭时间”
     * 备注：这里不需要确认收款了，因为前面已经确认收款完毕了
     */
    public function closeOwnerByWillClose()
    {
        // 获取所有will_close为true的合同
        Owner::query()->where('will_close', true)->get()->each(function ($owner) {

            // 判断是否为最后一期 同时 判断当前合同是否结束
            if ($owner->lastItem->count == $owner->items->count() && Carbon::now()->gte($owner->ended_at)) {
                //合同关闭
                $owner->will_close = false;
                $owner->closed = true;
                $owner->save();

                // 解除合同和房间的依赖，把房间设置为可以出租
                (new EstateService())->changeIsRent($owner);
            } else {
                Log::error("代码异常：当前合同不是最后一期的账单，但是will_close却为true");
            }
        });
    }

    public function expires()
    {
        $model = Owner::query()->first();
        $model->ended_at = $model->started_at->modify('-2 days');
        $model->save();

        Owner::query()->where('will_close', true)->get()->each(function ($owner) {
            // 判断是否为最后一期 同时 判断当前合同是否结束
            if ($owner->lastItem->count == $owner->items->count() && \Illuminate\Support\Carbon::now()->gte($owner->ended_at)) {
                //合同关闭
                $owner->will_close = false;
                $owner->closed = true;
                $owner->save();

                // 解除合同和房间的依赖，把房间设置为可以出租
                (new EstateService())->changeIsRent($owner);
            } else {
                Log::error("代码异常：当前合同不是最后一期的账单，但是will_close却为true");
            }
        });

    }

}
